package com.xbongbong.paas.help.menu;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.manager.feign.ChartCategoryFeignClient;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryInitDTO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryInitVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.model.IndexSetModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 首页及报表报表分类初始化
 *
 * @Author: hongxiao
 * @date: 2020-12-23 11:20
 */
@Component
public class InitIndexConfigAndChartHelp {
    private static final Logger LOG = LoggerFactory.getLogger(InitIndexConfigAndChartHelp.class);

    @Resource
    private ChartCategoryFeignClient chartCategoryFeignClient;
    @Resource
    private IndexSetModel indexSetModel;

    public void initConfig(String corpid) throws XbbException {
        // 公司配置是否存在
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put("userId", "0");
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        int count = indexSetModel.getEntitysCount(param);
        if (count > 0) {
            return;
        }

        ChartCategoryInitDTO initDTO = new ChartCategoryInitDTO();
        initDTO.setCorpid(corpid);
        initDTO.setUserId("0");
        initDTO.setPlatform(PlatFormEnum.INTERNAL.getValue());

        Locale locale = LocaleContextHolder.getLocale();
        String lang = locale.toString();
        XbbResponse<ChartCategoryInitVO> initVOXbbResp = chartCategoryFeignClient.chartCategoryInit(initDTO, lang);

        if (!initVOXbbResp.isSuccess()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018, initVOXbbResp.getMsg());
        }
    }
}
